package sc.common.test.cmd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.lang.ArrayUtils;

public class SimpleCommand {

    InputStream is;
    Commander commander;

    public SimpleCommand(InputStream is, Commander commander) {
        this.is = is;
        this.commander = commander;
    }

    public void start() {
        try {
            InputStreamReader is = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(is);
            while (true) {
                String cmd = reader.readLine();
                String[] cmds = cmd.split(" ");
                CommandContext ctxt = new CommandContext();
                if (cmds.length > 0) {
                    ctxt.setCommand(cmds[0]);
                }
                if (cmds.length > 1) {
                    ctxt.setArgs((String[]) ArrayUtils.subarray(cmds, 1, cmds.length));
                }
                commander.commandReceived(ctxt);
                if (ctxt.exit) {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
